var c = 0;
var t;
var isTimerOn = 0;

window.onload = function() {
	//disableAllButtonsOnTable();
};

/**
 * Update the computer's row
 * @param computer
 * @param result
 */
function updateComputer(computer, result) {
	computer.getElementsByTagName("td")[0].innerHTML = result.playGround.computer.jar;
	for ( var i = 1; i < 7; i++ ) {
		var td = computer.getElementsByTagName("td")[i];
		td.getElementsByTagName("input")[0].value = result.playGround.computer.row[i-1];
	}
}

/**
 * Update the player's row
 * @param player
 * @param result
 */
function updatePlayer(player, result) {
	for ( var i = 0; i < 6; i++ ) {
		var td = player.getElementsByTagName("td")[i+1];
		td.getElementsByTagName("input")[0].value = result.playGround.player.row[i];
	}
	player.getElementsByTagName("td")[7].innerHTML = result.playGround.player.jar;
}

/**
 * Whether there is a valid move for the player/computer or not
 * @param who player or computer
 * @return true if there is at least one valid move for who
 */
function canMove(who) {
	var answer = false;
	if (who == "player") {
		var playerRow = document.getElementById("Player");
		for ( var i = 1; i < 6; i++ ) {
			var coins = playerRow.getElementsByTagName("td")[i].getElementsByTagName("input")[0].value;
			if (coins > 0) {
				answer = true;
				break;
			}
		}
		
	} else if (who == "computer") {
		var computerRow = document.getElementById("Computer");
		for ( var i = 1; i < 6; i++) {
			var coins = computerRow.getElementsByTagName("td")[i].getElementsByTagName("input")[0].value;
			if (coins > 0) {
				answer = true;
				break;
			}
		}
	}
	if (!answer) {
		gameOver(who);
	}
}

/**
 * computer turn
 */
function moveComputer() {
	var computerRow = document.getElementById("Computer");
	for ( var i = 1; i < 6; i++) {
		var coins = computerRow.getElementsByTagName("td")[i].getElementsByTagName("input")[0].value;
		if (coins > 0) {
			console.log("Computer choose cell " + i + ".");
			updateGame("computer", i);
			break;
		}
	}
}

/**
 * Game over, calculate winner, coins, elapsed time and delayed forward to end page with appropriate parameters
 */
function gameOver() {
	var delay = 5000;
	var playerCoins = 0;
	var computerCoins = 0;
	var winner = "";
	var playerRow = document.getElementById("Player");
	var computerRow = document.getElementById("Computer");

	playerCoins = parseInt(playerRow.getElementsByTagName("td")[7].innerHTML);
	for ( var i = 1; i < 6; i++ ) {
		playerCoins += parseInt(playerRow.getElementsByTagName("td")[i].getElementsByTagName("input")[0].value);
	}
	computerCoins = parseInt(computerRow.getElementsByTagName("td")[0].innerHTML);
	for ( var i = 1; i < 6; i++) {
		computerCoins += parseInt(computerRow.getElementsByTagName("td")[i].getElementsByTagName("input")[0].value);
	}
		
	if (playerCoins > computerCoins) {
		winner = "player";
	} else if (playerCoins < computerCoins){
		winner = "computer";
	}
	
	console.log(playerCoins + " aggregated by player");
	console.log(computerCoins + " aggregated by computer");
	
	// assemble forward url with parameters
	if (playerCoins == computerCoins) {
		url = "end.jsp?tie=" + "true" + "&coins=" + playerCoins + "&time=" + c;
	} else if (winner == "computer") {
		url = "end.jsp?winner=" + winner + "&coins=" + computerCoins + "&time=" + c;
	} else if (winner == "player") {
		url = "end.jsp?winner=" + winner + "&coins=" + playerCoins + "&time=" + c;
	}
	
	stopGame();
	setTimeout(function () {
		window.location = url;
	}, delay);
}

/**
 * timer for elapsed time
 */
function timedCount() {
	document.getElementById('time_played').innerHTML = c;
	c = c + 1;
	t = setTimeout("timedCount()", 1000);
}

/**
 * initialize a new game
 */
function startGame() {
	console.log("start");
	c = 0;
	if (!isTimerOn) {
		isTimerOn = 1;
		timedCount();
	}
	newGame();
	return false;
}

/**
 * stop game
 */
function stopGame() {
	clearTimeout(t);
	isTimerOn = 0;
	return false;
}

function enableButton(button) {
	button.className = "button";
	button.disabled = "";
}

function enableButtons(buttons) {
	for ( var i = 0; i < buttons.length; i++) {
		enableButton(buttons[i]);
	}
}

function enableAllButtonsOnTable() {
	enableComputerButtons();
	enableComputerButtons();
}

function enableComputerButtons() {
	var computer = document.getElementById("Computer");
	enableButtons(computer.getElementsByTagName("input"));
}

function enablePlayerButtons() {
	var player = document.getElementById("Player");
	enableButtons(player.getElementsByTagName("input"));
}

function disableButton(button) {
	button.className = "button disabled";
	button.disabled = "disabled";
}

function disableButtons(buttons) {
	for ( var i = 0; i < buttons.length; i++) {
		disableButton(buttons[i]);
	}
}

function disableAllButtonsOnTable() {
	disableComputerButtons();
	disablePlayerButtons();
}

function disableComputerButtons() {
	var computer = document.getElementById("Computer");
	disableButtons(computer.getElementsByTagName("input"));
}

function disablePlayerButtons() {
	var player = document.getElementById("Player");
	disableButtons(player.getElementsByTagName("input"));
}
